﻿using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using D.UtilCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using CarAppDomain.CarType.IService;
using CarAppDto.CarType.CarDtos;
using CarAppDto.CarType.CarTypeDtos;
using D.UtilSqlsugarOrm;
using CarAppDto.CarParts.CarPartsDtos;

namespace CarAppAPI.CarType
{
    [Route("/api/[controller]/[action]")]
    [Authorize]
    public class CarTypeController : BaseApiController
    {
        private readonly ICarTypeService _service;
        public CarTypeController(ICarTypeService service)
        {
            _service = service;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddCarType reqModel)
        {
            // 验证和清理输入数据，防止注入攻击
            //var sanitizedComment = System.Web.HttpUtility.HtmlEncode(reqModel.Name);

            reqModel.CreateUser = Account;
            return await _service.AddCarTypeAsync(reqModel);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpDelete("{ids}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Delete(string ids)
        {
            var idsArray = ids.Split(",");
            return await _service.DeleteCarTypeAsync(idsArray.ToList());
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        [HttpPut, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update([FromBody] ReqUpdateCarType reqModel)
        {
            reqModel.UpdateUser = Account;
            return await _service.UpdateCarTypeAsync(reqModel);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepCarType>>> List([FromBody] ReqQueryCarTypeList reqModel)
        {
            return await _service.QueryCarTypeListAsync(reqModel);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<PageModel<RepCarType>>> Pages([FromBody] ReqQueryCarTypePage reqQueryPage)
        {
            return await _service.QueryCarTypePagesAsync(reqQueryPage);
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepCarType>> Get(string id)
        {
            return await _service.GetCarTypeAsync(id);
        }

        /// <summary>
        /// 获取单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepCarParts>>> GetCarPartsByCarTypeId(string id)
        {
            return await _service.GetCarPartsByCarTypeIdAsync(id);
        }
    }
}
